package com.bindapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.bindapp.R;

/**
 * Created by apple on 2018/7/10.
 */

public class LineProegress extends View {
    private Paint paintBoard,paintProgress,paintTxt;
    private Context mContext;
    private Paint.FontMetrics fontMetrics;

    private int boardwidth=2,boardcolor=R.color.seelbar,textsize=20;
    private String strMsg="20%";
    private float progress = 80;

    public LineProegress(Context context){
        this(context, null);
    }

    public LineProegress(Context context,AttributeSet attrs){
        this(context, attrs, 0);
    }

    public LineProegress(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initAttrs(attrs);
    }

    private void initAttrs(AttributeSet attrs){

        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.IndicatorSeekBar);
        boardwidth = ta.getInteger(R.styleable.LineProegress_boardwidth,2);
        boardcolor = ta.getColor(R.styleable.LineProegress_boardcolor, Color.parseColor("#67B8DE"));
        textsize = ta.getInteger(R.styleable.LineProegress_textSize,15);

        paintBoard=new Paint();
        paintBoard.setColor(boardcolor);
        paintBoard.setAntiAlias(false);
        paintBoard.setStrokeWidth(boardwidth);
        paintBoard.setStyle(Paint.Style.STROKE);

        paintProgress=new Paint();
        paintProgress.setColor(boardcolor);
        paintProgress.setAntiAlias(true);
        paintProgress.setStyle(Paint.Style.FILL);


        paintTxt=new Paint();
        paintTxt.setTextSize(textsize);
        paintTxt.setAntiAlias(true);
        paintTxt.setColor(Color.WHITE);
        paintTxt.setTextAlign(Paint.Align.CENTER);

        fontMetrics = paintTxt.getFontMetrics();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF=new RectF();
        rectF.left=0;
        rectF.right=getMeasuredWidth();
        rectF.top=0;
        rectF.bottom=getMeasuredHeight();
        canvas.drawRoundRect(rectF,10,10,paintBoard);


        LinearGradient linearGradient=new LinearGradient(0, 0, getMeasuredWidth()/100*progress, 0,new int[]{ContextCompat.getColor(mContext,R.color.seelbar),ContextCompat.getColor(mContext,R.color.progressblue)}, null, LinearGradient.TileMode.CLAMP);
        paintProgress.setShader(linearGradient);
        RectF rectP=new RectF();
        rectP.left=0;
        rectP.right=getMeasuredWidth()/100*progress;
        rectP.top=0;
        rectP.bottom=getMeasuredHeight();
        canvas.drawRoundRect(rectP,10,10,paintProgress);

        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        int baseLineY = (int) (rectP.centerY() - top/2 - bottom/2);
        canvas.drawText(strMsg,rectP.centerX(),baseLineY,paintTxt);

    }
}
